<?php
/**
 * Simple template class object.
 *
 * @author Lei Lee
 */
class Template {
	/**
	 * Template directory.
	 *
	 * @var string
	 */
	private $_template_dir = NULL;
	
	/**
	 * Template data set.
	 *
	 * @var array
	 */
	private $data = array();
	
	/**
	 * Constructor.
	 *
	 */
	function __construct() {
		
	}
	
	/**
	 * Pass values to template variables.
	 *
	 * @param string $var
	 * @param mixed $data
	 */
	function assign($var, $data) {
		$this->data[$var] = $data;
	}
	
	/**
	 * Print the template file.
	 *
	 * @param string $tpl_file
	 * @param boolean $exit
	 * @param array $extra_data
	 */
	function display($tpl_file, $exit = false, $extra_data = NULL) {
		$tpl_file = Router::standardize($this->_template_dir . DIRECTORY_SEPARATOR . $tpl_file);
		
		if (!file_exists($tpl_file)) 
			throw new FileNotFoundException('Template file does not exist.(File: ' . $tpl_file . ')', -1);
		
		if (is_array($extra_data)) {
			$this->data = array_merge($this->data, $extra_data);
		}
		
		include ($tpl_file);
		
		if ($exit) {
			exit(0);
		}
	}
	
	/**
	 * Set the template directory.
	 *
	 * @param string $template_dir
	 * @return Template
	 */
	function setTemplateDir($template_dir) {
		$this->_template_dir = $template_dir;
		return $this;
	}
}